// SCENARIO SCRIPT

// This is the special script for your entire scenario. It contains
// special encounters and code accessable from anywhere in the scenario.  It
// also contains the code that initializes important special things in the
// scenario (like shops and names and descriptions of special items).

// You can create your own states, but you should give all of them numbers
// greater than or equal to 10.

beginscenarioscript;

variables;

    short user;
    short locX;
    short locY;
    short MELVILLE = 6; // Character # for Melville.
    short monster;
    short DEATH = 6; // Character # for Death.

body;

// This is the state that is called every time the scenario is loaded,
// even when a save file in the scenario is loaded. Some things that should go
// here:
//    Names and descriptions of special items.
//    Names and effects of custom special abilities.

beginstate LOAD_SCEN_STATE;
	break;

// This is the state that is called only once at the very beginning of 
// the scenario. Some things that should go here:
//    The stuff in shops.
//    Creating horses and boats.

beginstate START_SCEN_STATE;

    // Create our boat.

    create_boat(1, 1, 19, 18, 0);

    // Change the starting time-of-day so that we don't start the scenario at
    // dawn.

    set_ticks_forward(1700); // Skips to start to day.

    // Create our special items.

    init_special_item(1, "Doss-house Key", "This small and rusty key has 'Doss-House' carved into the shank.");
    init_special_item(2, "Official Letter", "This letter is addressed to Malone,the Empire overseer in Olsen.");

    // Initialise the Olsen general store.

    add_item_to_shop(1,  4,  500); // Bread.
    add_item_to_shop(1,  8,  500); // Dried meat.
    add_item_to_shop(1,  26,   3); // Leather armour.
    add_item_to_shop(1,  30,   1); // Poor studded armour.
    add_item_to_shop(1,  46,   1); // Iron dagger.
    add_item_to_shop(1,  55,   1); // Bronze longsword.
    add_item_to_shop(1,  65,   1); // Crude spear.
    add_item_to_shop(1, 121,   2); // Wooden buckler.
    add_item_to_shop(1, 138,   1); // Fine leather helmet.
    add_item_to_shop(1, 154,   3); // Bolt of cloth.
    add_item_to_shop(1, 174,   2); // Lockpicks.
    add_item_to_shop(1, 177,   3); // First aid kit.
    add_item_to_shop(1, 187,   1); // Scalpel.
    add_item_to_shop(1, 206,   1); // Silver ring.
    add_item_to_shop(1, 220,   3); // Healing potion.
    add_item_to_shop(1, 282,   1); // Wand of bolts.

break;

// This state is called every tick wherever the party is in the scenario.

beginstate START_STATE;
break;

// The state is called when the player uses the Death Charm.

beginstate 10;
    if (get_flag(10, 2) == 0) {
        reset_dialog_preset_options(0);
        add_dialog_str(0, "You try to use the Death Charm, but you're not sure how it works.  Nothing happens.", 0);
        run_dialog(1);
    } else {
        // If we are outdoors (either fighting outdoors, or just walking
        // about), tell the player that the death charm has no effect.

        if (is_outdoor() || (get_flag(10, 13) == 1)) {
            play_sound(41); // Failure.
            pause(5);
            reset_dialog_preset_options(0);
            add_dialog_str(0, "You perform the ritual to activate the charm, but it doesn't seem to have any effect.  The charm doesn't seem to work outside for some reason.", 0);
            run_dialog(1);
            end();
        }

        play_sound(24);

        // If this is the first time the player used the charm, describe the
        // effect the charm has.

        if (get_flag(10, 3) == 0) { // First use.
            reset_dialog_preset_options(0);
            add_dialog_str(0, "You perform the ritual Fran taught you.  The Death Charm glows angrily, then slowly fades.  It has an effect on any undead nearby.", 0);
            run_dialog(1);
            set_flag(10, 3, 1);
        } else {
            print_str_color("You activate the Death Charm...", 2);

        // Send our creature number to all undead.  They will give themselves
        // some damage if they are close enough.  Note that all the undead
        // are in group #1.

        user = who_used_custom_item();
        locX = char_loc_x(user);
        locY = char_loc_y(user);

        give_char_message(1001, user);

        // If Death is nearby, tell the player that this is futile.  Everyone
        // dies.

        if (current_town() == 3) { // In storage rooms.
            if ((locX >= 35) && (locX <= 48) && (locY >= 11) && (locY <= 24)) {
                // In Death's Domain.
                if (char_status(DEATH) == 1) { // Death still there.
                    reset_dialog_preset_options(0);
                    add_dialog_str(0, "You raise the Charm and frantically perform the ritual to activate it.", 0);
                    run_dialog(1);

                    pause(5);
                    put_boom_on_char(DEATH, 0, 0);
                    run_animation_sound(5);
                    pause(10);

                    reset_dialog_preset_options(0);
                    add_dialog_str(0, "The sound of Death's laughter fills you with despair.", 0);
                    add_dialog_str(1, "_Ha!  You think you can kill Death?  Do you not know that death cannot be destroyed?_", 0);
                    add_dialog_str(2, "_Come, come to me.  It is time._", 0);
                    run_dialog(1);
                    kill_char(1000, 2, 0); // Kill entire party.
                    end();
                }
            }
        }

        // If Melville is nearby, and isn't undead or destroyed, reveal him.

        if (current_town() == 2) { // In school.
            if ((locX >= 30) && (locX <= 34) && (locY >= 23) && (locY <= 27)) {
                // In Melville's room.
                if (party_can_see_loc(32, 25)) { // Can see inside room.
                    if (char_status(MELVILLE) == 1) { // Melville still there.
                        put_boom_on_char(MELVILLE, 0, 0);
                        run_animation_sound(5);
                        damage_char(MELVILLE, get_ran(3, 1, 8), 4);

                        reset_dialog_preset_options(0);
                        add_dialog_str(0, "Melville stares at you.  _That hurt!_", 0);
                        add_dialog_choice(0, "But...but...the charm only affects undead.");
                        run_dialog(1);

                        pause(10);
                        relocate_character(MELVILLE, 33, 23);
                        set_character_facing(MELVILLE, 2);

                        march_party(32, 25);
                        force_instant_terrain_redraw();
                        pause(2);

                        march_party(31, 25);
                        force_instant_terrain_redraw();
                        pause(2);

                        march_party(31, 24);
                        force_instant_terrain_redraw();
                        pause(2);

                        march_party(31, 23);
                        force_instant_terrain_redraw();
                        pause(2);

                        play_sound(48); // Melville goes beserk.
                        pause(10);

                        reset_dialog_preset_options(0);
                        add_dialog_str(0, "Melville groans and falls to his knees.  _But that means..._", 0);
                        add_dialog_str(1, "He howls.  _They took me!  The foul brutes.  It must have happened that first night.  I never knew: I've been one of them all along!_", 0);
                        add_dialog_str(2, "As you stand before him, he begins to change...", 0);
                        run_dialog(1);

                        locX = char_loc_x(MELVILLE);
                        locY = char_loc_y(MELVILLE);

                        put_boom_on_char(MELVILLE, 6, 0);
                        run_animation_sound(102);

                        pause(2);

                        erase_char(MELVILLE);
                        place_monster(locX, locY, 235, 0);
                        set_creature_memory_cell(char_on_spot(locX, locY),
                                                 1, 15); // Call state 15 when
                                                         // monster killed.

                        put_boom_on_space(locX, locY, 6, 0);
                        run_animation_sound(112);

                        flip_terrain(32, 26); // Lock door.
                        play_sound(59);
                        force_instant_terrain_redraw();
                        pause(10);

                        reset_dialog_preset_options(0);
                        add_dialog_str(0, "Hmmm...looks like you've got a fight on your hands!", 0);
                        run_dialog(1);
                    }
                }
            }
        }
    }
break;

// The following state is called when the player reads a book.

beginstate 11;
    reset_dialog_preset_options(0);
    add_dialog_str(0, "This book has an awful lot of words in it.  It's not very interesting.", 0);
    run_dialog(1);
break;

// The following state is called when the player reads a large book.

beginstate 12;
    reset_dialog_preset_options(0);
    add_dialog_str(0, "This large book describes the use of magical forces for both good and evil ends.  You find it interesting, but the contents are far too esoteric for you to use.", 0);
    run_dialog(1);
break;

// The following state is called when the player reads a textbook.

beginstate 13;
    reset_dialog_preset_options(0);
    add_dialog_str(0, "This is an alchemy textbook.  You read through, and learn more about alembics and the transformation of elements then you ever wanted to know.  It's not very enlightening.", 0);
    run_dialog(1);
break;

// The following state is called when the player reads a journal.

beginstate 14;
    reset_dialog_preset_options(0);
    add_dialog_str(0, "This is a student's journal.  It is filled with tedious descriptions of exams and endless study.", 0);
    add_dialog_str(1, "What a boring lot these students were!", 0);
    run_dialog(1);
break;

// The following state is called when the player reads a novel.

beginstate 15;
    reset_dialog_preset_options(0);
    add_dialog_str(0, "The adventuring life is much more interesting than anything you'll find in this novel!", 0);
    run_dialog(1);
break;

// ##########################################################################
// vim: wrap linebreak textwidth=0 shiftwidth=4 syntax=cpp
